#!/usr/bin/env python
#conding=UTF-8
#Ver:0.2@2007-08-25
import sys, random, string, time
def readarguments():
    argts=["","",""]
    if len(sys.argv) == 1:
        arg="none"
    else:
        arg="yes"
        l=0
        for i in sys.argv:
            argts[l]=i
            l+=1
    return arg, argts
def main():
    argt=readarguments()
    if argt[0] == 'yes':
        #print argt[1][1],argt[1][2]
        if argt[1][1] == 'Destroy':
            destroy()
        elif argt[1][1] == 'open':
            main_type(argt[1][2])
    elif argt[0] == 'none':
        main_type(".dataf")
    else:
        print >> sys.stderr, "Error!"
        sys.exit(1)
def destroy():
    print 'randomize'
    print 'Destroy Mode!------>Started!'
    wordog=list(string.ascii_lowercase+" "+" "+" "+" "+" ")
    ee=0
    c=0
    start=time.time()
    while not ee :
        word=""
        for i in range(29):
            word+=random.choice(wordog)
        print word
        input=raw_input()
        c+=len(input)
        if input == 'ExIt': ee=1
    end=time.time()
    print 'Spend:  ', start-end, " s"
    print 'Total put ', c, ' keys'
def main_type(dataf):
    rec=[]
    total_w=0
    print 'Start read file include words.'
    try :
         dafi = open( dataf )        #try to open file
    except IOError, message:
         print >> sys.stderr, 'File not Found!!\nCheck that    has a file name ".dataf" in working path.'        #print error if file not found.
         sys.exit(1)        #exit with error!
    f_records=dafi.read()        #read file by lines.
    f_records=f_records.splitlines()        #split records to several lines.
    dafi.close()        #close file after read.
    #print f_records
    start=time.time()
    for i in f_records:        #print each lines in records.
        i=i.strip()
        print '================================================'
        print "  |" + i + "|__Enter__"
        word = raw_input( '^:|' )
        c=0
        while i != word:        #check that right or wrong
            c+=1
            if word.lower() == 'exit':    sys.exit()
            ww=len(word)
            wwi=len(i)
            whyy='Nothing is wrong~'
            if ww != wwi:
                whyy='typed words not enough.'
            elif ww == wwi:
                wrong_list=[]
                whyy=""
                for x in range(ww):
                    if word[x] != i[x]:    wrong_list+= [ x ]
                for x in wrong_list:
                    whyy=whyy + i[int(x)] + " | "
            print 'Wrong!!!   ', whyy
            print '\n\n\n================================================'
            print "  |" + i + "|__Enter__"
            word = raw_input( '^:|' )
            if c >= 3:    word =i
        total_w += len(word)
    end=time.time()
    print '\n\n\n==========End=========='
    print 'Spend %ds.' % (end - start)
    print 'Total press %d keys.' % total_w
    print 'Speed: %d keys/s.' % ( total_w / (end - start) )
def main_old():
    rec = []
    print 'Start read anima file.'
    try:
        dafi = open( ".dataf" )
    except IOError, message:
        print >> sys.stderr, "File could not Open!!\nPlease check it!"
        sys.exit(1)
    anima_records = dafi.read()        #read words from dafi
    lines = 0
    for anima_record in anima_records:      #read each line of anima records
        anima = anima_record.split()        #split the anima records to one line
        lines = lines + 1
        rec += anima        #print the anima record
    dafi.close()
    print "There have " + str( lines ).center(3) + " lines in the file."
    n = lines
    for i in rec:
        n -= 1
        print '[  ' + str( n ) + '  ]',
        print '================================================'
        print " ",
        print i
#        print "HHH"
        word = raw_input('^:')
        while rec[i] != word:
            if word == 'Exit':  sys.exit()
            if word == 'exit': sys.exit()
            print 'type wrong, please type again.'
            print '\n\n\n================================================'
            print " ",
            print i
            word = raw_input('^:')
    print "The End!!"
if __name__ == "__main__":
    main()
    sys.exit()
